<template>
	<el-container>
<!--		<el-header style="height: auto;">-->
<!--			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>-->
<!--		</el-header>-->
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="runId" stripe remoteSort remoteFilter>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="流水号" prop="oaReimbursements.id" width="150"></el-table-column>
				<el-table-column label="报销事由" prop="oaReimbursements.subjectMatter" width="150"></el-table-column>
				<el-table-column label="费用汇总" prop="oaReimbursements.costAggregation" width="150" ></el-table-column>
				<el-table-column label="申请人" prop="applicantName" width="150"></el-table-column>
				<el-table-column label="电子签名" align="center">
					<template v-slot="scope">
						<div v-if="scope.row.signatureUrl">
							<el-image
								style="width: 80px; height: 40px; cursor: pointer"
								:src="scope.row.signatureUrl"
								:preview-src-list="[scope.row.signatureUrl]"
								fit="contain"
								close-on-click-modal
								preview-teleported
								:z-index="9999"
							>
							</el-image>
						</div>
						<span v-else class="unsigned-text">未签署</span>
					</template>
				</el-table-column>
				<el-table-column label="申请人部门" prop="applicantDeptName" width="150"></el-table-column>
				<el-table-column label="审批人" prop="approverName" width="150"></el-table-column>
				<el-table-column label="审批人部门" prop="approverDeptName" width="150"></el-table-column>
				<el-table-column label="状态" prop="stats">
					<template #default="scope">
						<span v-if="scope.row.stats === '0'">审核中</span>
						<span v-else-if="scope.row.stats === '1'">通过</span>
						<span v-else-if="scope.row.stats === '2'">驳回</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button-group>
<!--							<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">查看</el-button>-->
							<el-button-group>
								<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">查看费用报销明细</el-button>
							</el-button-group>
							<el-button text type="primary" size="small" v-if="scope.row.stats === '0'" @click="openApprovalDialog(scope.row)">审批</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>

			<el-dialog v-model="approvalDialogVisible" title="审批" width="40%">
				<el-form :model="approvalFormData" label-width="100px">
					<el-form-item label="审批结果">
						<el-radio-group v-model="approvalFormData.stats">
							<el-radio label="1">同意</el-radio>
							<el-radio label="2">拒绝</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="备注">
						<el-input v-model="approvalFormData.remark" type="textarea" :rows="4"></el-input>
					</el-form-item>
					<div>
						<spersonalFinance
							ref="signaturePad"
							:line-width="4"
							:line-color="'#409EFF'"
							@save="handleSave"
							@error="handleError"
							@reset="reset"
						></spersonalFinance>
					</div>

<!--					<div>-->

<!--						<el-card class="qianming-container" body-style="padding:0px">-->
<!--							<vue-esign ref="esign"  :isCrop="isCrop" :width="600" :height="300" :lineWidth="lineWidth" :lineColor="lineColor" ></vue-esign>-->
<!--							<div class="contro-container">-->
<!--								<el-button type="danger"  @click="handleReset">清空画板</el-button>-->
<!--								<el-button type="primary" @click="handleGenerate">生成图片</el-button>-->
<!--							</div>-->
<!--						</el-card>-->
<!--						<el-card class="box-card">-->
<!--							<div  class="clearfix">-->
<!--								<span>请确认签名</span>-->
<!--							</div>-->
<!--							<div class="text item">-->
<!--								<img :src="resultImg" class="show-img" v-if="resultImg != ''">-->
<!--								<div class="show-info" v-else>请在下方书写电子签名</div>-->
<!--							</div>-->
<!--						</el-card>-->
<!--					</div>-->
				</el-form>
				<template #footer>
					<el-button @click="approvalDialogVisible = false">取消</el-button>
					<el-button type="primary" @click="submitApproval">提交</el-button>
				</template>
			</el-dialog>
			<el-dialog v-model="dialogVisible" title="费用明细" width="60%">
				<el-table :data="oaFinanceDetails">
					<el-table-column label="id" prop="id" ></el-table-column>
					<el-table-column label="内容" prop="content" ></el-table-column>
					<el-table-column label="金额" prop="amount" ></el-table-column>
				</el-table>
				<template #footer>
					<el-button @click="dialogVisible = false">关闭</el-button>
				</template>
			</el-dialog>
		</el-main>
	</el-container>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import spersonalFinance from '@/components/scpersonalFinance/index.vue'
import { FINANCE_CODES } from '@/config/statusCodes'
export default {
	name: 'My Attend Record',
	components: {
		scSelectFilter,
	spersonalFinance
	},
	data() {
		return {
			// lineWidth: 6,
			// lineColor: '#000000',
			// bgColor: '',
			resultImg: '',
			isCrop: false,
			dialogVisible: false, // 控制弹窗显示
			oaFinanceDetails: [], // 存储费用报销明细数据
			approvalDialogVisible: false, // 控制审批弹窗显示
			approvalFormData: {},
			currentRow: null, // 当前行数据
			dialog: {
				save: false,
				detailsSave: false
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "请假类型",
					key: "attendType",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "事假",
							value: "A"
						},
						{
							label: "婚假",
							value: "B"
						},
						{
							label: "丧假",
							value: "C"
						},
						{
							label: "年价",
							value: "D"
						}
					]
				}
			],
			apiObj: null,
			search: {
				attendType:'',
				date: '',
				page: 1,
				pageSize: 15
			}
		}
	},
	mounted() {
		this.initApiObj(); // 初始化时调用
	},
	methods: {
		reset(){
			this.resultImg = ''
		},
		// 处理签名保存
		handleSave({ base64, file }) {
			// 自定义上传逻辑
			const formData = new FormData()
			formData.append('file', file)
			formData.append('code', FINANCE_CODES.EXPENSE_REIMBURSEMENT)

			this.$API.office.personalFinance.uploadFileByCode.post(formData)
				.then(res => {
					this.resultImg  = res.redirect
				})
				.catch(err => {
					this.$message.error('文件上传失败')
				})
		},
		// 处理错误
		handleError(message) {
			this.$message.error(message)
		},

		openApprovalDialog(row) {
			this.currentRow = row;
			this.approvalDialogVisible = true;
			this.approvalFormData.stats = '';
			this.approvalFormData.remark = '';
			if (this.$refs.signaturePad) {
				this.$refs.signaturePad.handleReset();
			}
		},
		submitApproval() {
			if (!this.approvalFormData.stats) {
				this.$message.error('请选择审批结果');
				return;
			}
			if (
				typeof this.resultImg === 'undefined' ||
				this.resultImg === null ||
				(typeof this.resultImg === 'string' && this.resultImg.trim() === '')
			) {
				this.$message.error('请生成签名后或字迹规范后审批');
				return;
			}
			// 提交审批逻辑
			let params = {
				correlationId: this.currentRow.correlationId,
				stats: this.approvalFormData.stats,
				remark: this.approvalFormData.remark,
				signatureUrl:this.resultImg
			};
			// this.approvalFormData.id = this.currentRow.id;
			console.log(this.approvalFormData)
			this.$API.office.personalFinance.reimbursementsApprove.get(params).then(res => {
				this.$refs.signaturePad.handleReset();
				this.$message.success('审批成功');
				this.approvalDialogVisible = false;
				this.initApiObj();

			}).catch(err => {
				this.$refs.signaturePad.handleReset();
				this.$message.error('审批失败');
				console.error("接口调用失败", err);
			});
		},
		// 初始化apiObj
		initApiObj() {
			const userInfo = this.$TOOL.data.get("USER_INFO");
			this.apiObj = {
				get: async (params) => {
					// 合并固定参数（userInfo.id）和动态参数（this.search）
					const mergedParams = {
						option2: userInfo.id,
						...this.search,
						...params // 允许外部传入的params覆盖默认值
					};
					return await this.$API.office.personalFinance.reimbursementsGetByApprover.get(mergedParams);
				}
			};
		},
		// 筛选条件变化时更新参数并刷新表格
		filterChange(data) {
			this.search.date = data.date;
			this.search.attendType = data.attendType;
			this.$refs.table.upData(this.search); // 触发表格重新加载
		},
		// filterChange(data) {
		// 	this.search.date = data.date;
		// 	this.$refs.table.upData(this.search)
		// },
		readBpmDetails(row,index){
			this.oaFinanceDetails = row.oaReimbursementDetails;
			this.dialogVisible = true; // 显示弹窗
		},
	}
}
</script>
<style scoped>
</style>
